package webserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.98D3F829-B393-054A-FCF6-781E2246590C]
// </editor-fold>
/**
 * Class AppProperties providing access to the Properties
 * @author Andi
 */
public class AppProperties {

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.897C2446-68A7-0EF4-5AD3-1668D369F10C]
	// </editor-fold>
	// Static Declarations
	private static Properties prop;
	private static AppProperties instance = null;
	private File settings;

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.E1B02E35-3F68-75D8-D7BE-8CA37123E4F8]
	// </editor-fold>
	/**
	 * AppProperties Default Constructor
	 * Private for Singleton-DesignPattern
	 */
	private AppProperties() {
		prop = new Properties();

		this.settings = new File("settings.ini");
		try {
			FileInputStream reader = new FileInputStream(settings);
			prop.load(reader);
			Logger.writeToLog("Settings loaded: " + prop.toString());
			reader.close();
		} catch (FileNotFoundException fnfe) {
			Logger.writeToLog("Settings file not found: " + fnfe.toString());
		} catch (IOException io) {
			Logger.writeToLog("IO Error reading Settings: " + io.toString());
		}

	}

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.1809EC81-7D44-2F27-F5B7-7A673EA18DAC]
	// </editor-fold>
	/**
	 * Get the current instance of AppProperties
	 * @return AppProperties
	 */
	public static AppProperties getInstance() {
		if (instance == null) {
			instance = new AppProperties();
		}

		return instance;
	}

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.3FD96F8A-4BB6-2622-A1DF-EADEE525A663]
	// </editor-fold>
	/**
	 * Get the port
	 * @return int - port
	 */
	public int getPort() {
		return Integer.parseInt(prop.getProperty("port", "8080"));
	}

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.21257EFB-79F1-D81C-4C16-3FB9DDE63D07]
	// </editor-fold>
	/**
	 * Set the port
	 * @param port
	 */
	public void setPort(int port) {
		prop.setProperty("port", "" + port);
	}

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.21B28973-7ECF-6DB5-4199-4F095F708917]
	// </editor-fold>
	/**
	 * Get the path to Home Directory
	 * @return String - Path
	 */
	public String getHomeDirectory() {
		return prop.getProperty("homeDir");
	}

	// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
	// #[regen=yes,id=DCE.A55DD2CD-34D9-7131-7E22-91CFBF634CE1]
	// </editor-fold>
	/**
	 * Set the path to Home Directory
	 * @param val - path
	 */
	public void setHomeDirectory(String val) {
		prop.setProperty("homeDir", val);
	}

	/**
	 * Get the path to Error Directory
	 * @return String - path
	 */
	public String getErrorDirectory() {
		return prop.getProperty("errorDir");
	}

	/**
	 * Set the path to Error Directory
	 * @param val - path
	 */
	public void setErrorDirectory(String val) {
		prop.setProperty("errorDir", val);
	}

	/**
	 * Get the maximum number of response Threads to use
	 * @return int
	 */
	public int getResponseThreadCount() {
		return Integer.parseInt(prop.getProperty("maxResponseThreads", "5"));
	}

	/**
	 * Set the maximum number of responde Threads to use
	 * @param val - number of Threads
	 */
	public void setResponseThreadCount(int val) {
		prop.setProperty("maxResponseThreads", "" + val);
	}

	/**
	 * Return the interval when the Cache should be reagularly checked for invalid entries
	 * @return int - time in ms
	 */
	public int getCacheManagementInterval() {
		return Integer.parseInt(prop.getProperty("cacheInvalidationCheckInterval", "5000"));
	}

	/**
	 * Set the intarval in which the Cache should be checked for invalid entries
	 * @param val - time in ms
	 */
	public void setCacheManagementInterval(int val) {
		prop.setProperty("cacheInvalidationCheckInterval", "" + val);
	}

	/**
	 * Get the interval after which a "100 continue" should be sent as Keep-Alive
	 * @return int - time in ms
	 */
	public int getKeepAliveWaitInterval() {
		return Integer.parseInt(prop.getProperty("keepAliveInterval", "2000"));
	}

	/**
	 * Set the interval after which a KeepAlive should be sent
	 * @param val - time in ms
	 */
	public void setKeepAliveWaitInterval(int val) {
		prop.setProperty("keepAliveInterval", "" + val);
	}

	/**
	 * Save the settings to a file
	 */
	public void save() {
		try {
			FileOutputStream out = new FileOutputStream(this.settings);
			prop.store(out, "");
			out.close();
		} catch (FileNotFoundException fnfe) {
			Logger.writeToLog("Cannot find Settings File:" + fnfe.toString());
		} catch (IOException io) {
			Logger.writeToLog("IO Error wrinting Settings: " + io.toString());
		}
	}

	/**
	 * Get the default Domain which should be used if no other has been set
	 * @return String
	 */
	public String getDefaultDomain() {
		return prop.getProperty("defaultDomain");
	}

	/**
	 * Set the default domain to use
	 * @param domain
	 */
	public void setDefaultDomain(String domain) {
		prop.setProperty("defaultDomain", domain);
	}
}

